package com.health.healthproject.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.health.healthproject.entity.Announcement;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

@Data
public class AnnouncementDTO {

    private Long id;

    @NotBlank(message = "公告标题不能为空")
    @Size(max = 100, message = "公告标题最多100个字符")
    private String title;

    @NotBlank(message = "公告内容不能为空")
    private String content;

    private List<String> attachments;

    private Integer status;

    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    private Long createBy;

    private Long updateBy;

    private String createName;

    private String fileName;

    // 转换为实体对象
    public Announcement toEntity() {
        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(this, announcement);
        announcement.setAttachmentList(attachments);
        return announcement;
    }

    // 从实体对象转换为DTO
    public static AnnouncementDTO fromEntity(Announcement announcement) {
        if (announcement == null) {
            return null;
        }
        AnnouncementDTO dto = new AnnouncementDTO();
        BeanUtils.copyProperties(announcement, dto);
        dto.setAttachments(announcement.getAttachmentList());
        return dto;
    }
}